// pages/detail-songs/index.js
import { recommendStore } from "../../store/index"
import { getPlayListDetail } from '../../service/api_music'
import { getMySonglistDetail } from "../../service/api_user"
import {getUserRecord,getUserCloud} from "../../service/api_user"
import {getPodcastProgram} from "../../service/api_podcast"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ranking: '',  // 当前传入的榜单名
    dataInfo: {}, // 榜单/歌单数据
    listId: '',    // 歌单id
    mylistdetail: {}, // 我的歌单数据
    record:[],    // 一周内播放记录
    mycloud:[],   // 我的云盘内容
    programs:[],   // 播客界面数据
    title:"",     // 标题
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getPageData(options)
  },
  async getPageData(options) {
    // 判断是歌单还是榜单
    if (options.listId && !options.mylist) { // 歌单
      this.setData({ listId: options.listId })
      const res = await getPlayListDetail(options.listId)
      this.setData({ dataInfo: res.data.playlist })
    } else if (options.ranking && !options.mylist) {// 榜单
      const rankingMap = { "热歌榜": "hotRanking", "飙升榜": "upRanking", "新歌榜": "newRanking", "原创榜": "originRanking", }
      this.setData({ ranking: rankingMap[options.ranking] })
      // 获取数据 
      recommendStore.onState(rankingMap[options.ranking], this.getRankingdataHandler)
    } else if (options.listId && options.mylist) { // 我的歌单
      const res = await getMySonglistDetail(options.listId)
      this.setData({ mylistdetail: res.data.playlist })
    }else if(options.mycloud){  // 我的云盘
      const res = await getUserCloud()
      const tempArr = res.data.data
      const mycloud = []
      tempArr.forEach(item=>{
        item.simpleSong.iscloud = true
        mycloud.push(item.simpleSong)
      })
      this.setData({mycloud})
    }else if(options.title){   // 播客
      const res = await getPodcastProgram(options.podcastId)
      const temp = res.data.programs
      const programs = []
      temp.forEach(item=>{
        programs.push(item.mainSong)
      })
      this.setData({programs,title:options.title})
    }else{  //最近一周播放
      const res = await getUserRecord(wx.getStorageSync('userId'))
      const weekData = res.data.weekData
      const record = []
      weekData.forEach(element => {
        record.push(element.song)
      });
      this.setData({record})
    }
  },
  onUnload() {
    if (this.data.ranking) {
      // 销毁页面时候 取消数据绑定
      recommendStore.offState(this.data.ranking, this.getRankingdataHandler)
    }
  },
  getRankingdataHandler(res) {
    this.setData({ dataInfo: res })
  }
})